﻿using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MysqlforDataWatch;
using RainFlowandDamageTool.ComputingProcess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Tools.AddDistance;
using Tools.MyConfig;
using static Tools.FileOperation.CSVFileImport;

namespace Tools.ListOperation.StatisticAccListOperation
{
    public static class AddStatisticacc
    {
      
        /// <summary>
        /// 添加所有通道的统计值，除了前面3列
        /// </summary>
        /// <param name="columncount"></param>
        /// <param name="tablehead"></param>
        /// <param name="dt"></param>
        /// <param name="vehicleid"></param>
        /// <param name="name"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static List<T> addStatisticAllList <T>(CSVStatisticDataAllImport cSVStatisticDataAllImport,string vehicle) where T : StatisticBase, new()
        {

            List<T> statisticall = new List<T>();
            double nextmileage = 0;
            //第一列先不管
            for (int i = 1; i < cSVStatisticDataAllImport.columncount; i++)
            {
                T statisticentity = new T();
                statisticentity.vehicle = vehicle;
                statisticentity.filename = cSVStatisticDataAllImport.name;
                statisticentity.datadate = Convert.ToDateTime(cSVStatisticDataAllImport.datetime);
                statisticentity.chantitle = cSVStatisticDataAllImport.headers[i];
                statisticentity.max= cSVStatisticDataAllImport.lists[i].Max();
                statisticentity.min = cSVStatisticDataAllImport.lists[i].Min();
                statisticentity.range = Math.Abs(((double)statisticentity.max - (double)statisticentity.min));
                double sumOfSquares = cSVStatisticDataAllImport.lists[i].Sum(x => x * x);
                double meanSquare = sumOfSquares / (cSVStatisticDataAllImport.lists[i].Count-1);
                statisticentity.rms = Math.Sqrt(meanSquare);
                double mean = cSVStatisticDataAllImport.lists[i].Average();
                statisticentity.mean = mean;
                double variance = cSVStatisticDataAllImport.lists[i].Sum(x => Math.Pow(x - mean, 2)) / (cSVStatisticDataAllImport.lists[i].Count - 1);
                statisticentity.variance = variance;
                statisticentity.std = Math.Sqrt(variance);
                statisticentity.damagek3= cSVStatisticDataAllImport.lists[i].GetAccumDamagefromList(3, 3);
                statisticentity.damagek5 = cSVStatisticDataAllImport.lists[i].GetAccumDamagefromList(5, 5);

                if (cSVStatisticDataAllImport.headers[i].Contains("vehspd"))
                {
                    //计算里程
                    var singledistance = AddDistanceList.ReturnSingleDistance(cSVStatisticDataAllImport.lists[i], cSVStatisticDataAllImport.lists[0]);
                    nextmileage = singledistance.Sum();
                }
                statisticentity.mileage = nextmileage;

                statisticall.Add(statisticentity);
            }
            foreach (var item in statisticall)
            {
                item.mileage = nextmileage;
            }

            return statisticall;
        }

    }
}
